package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysSellSetting;
import cn.stylefeng.guns.modular.yingbao.mapper.SysSellSettingMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysSellSettingParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysSellSettingResult;
import  cn.stylefeng.guns.modular.yingbao.service.SysSellSettingService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 买入超时设置 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-13
 */
@Service
public class SysSellSettingServiceImpl extends ServiceImpl<SysSellSettingMapper, SysSellSetting> implements SysSellSettingService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysSellSettingParam param){
        SysSellSetting entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysSellSettingParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysSellSettingParam param){
        SysSellSetting oldEntity = getOldEntity(param);
        SysSellSetting newEntity = getEntity(param);
        newEntity.setOverTime(newEntity.getOverTime()*60*60);
        newEntity.setStandby2(newEntity.getStandby2()*60*60);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysSellSettingResult findBySpec(SysSellSettingParam param){
        return null;
    }

    @Override
    public List<SysSellSettingResult> findListBySpec(SysSellSettingParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysSellSettingParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(SysSellSettingParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysSellSetting getOldEntity(SysSellSettingParam param) {
        return this.getById(getKey(param));
    }

    private SysSellSetting getEntity(SysSellSettingParam param) {
        SysSellSetting entity = new SysSellSetting();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
